package com.woniuxy.controller;

import com.woniuxy.bean.ResultDto;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController extends BaseController {
    @Autowired
    private UserService userService;
    @GetMapping(value = "/login.do")
    public ResultDto<User> login(@RequestParam("telephone") String telephone,
                                 @RequestParam("password") String password){
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(telephone,password);
        subject.login(token);
        if(subject.isAuthenticated()){
            User user=userService.login(telephone,password);
            //获取Session对象
            Session session=subject.getSession();
            session.setAttribute("USER_SESSION",user);
            return new ResultDto(1001,"登录成功",null,user);
        }else{
            return new ResultDto(-1001,null,"用户名或者密码有误",null);
        }
    }
}
